package mya_dc.shared_classes;

import java.io.File;
import java.io.Serializable;
import java.util.Date;

/**
 * An extension of the built in java.io.File that supports a marking
 * of 'last transmission'. 
 *
 * @author      Adam Levi
 * <br>			MYA
 */

public class MYA_File extends java.io.File implements Serializable
{

	private static final long serialVersionUID = 1L;
	
	/**
	 * validates the name of the given filename.
	 * 
	 * @param filename - a filename
	 *
	 * @return true <==> filename does not contain one of the the following
	 * characters (\,/,*,?,<,>,|,")
	 */
	public static boolean validFilename(String filename)
	{
		if (
				filename.indexOf("\\") != -1 ||
				filename.indexOf("/") != -1 ||
				filename.indexOf(":") != -1 ||
				filename.indexOf("*") != -1 ||
				filename.indexOf("?") != -1 ||
				filename.indexOf("<") != -1 ||
				filename.indexOf(">") != -1 ||
				filename.indexOf("|") != -1 ||
				filename.indexOf("\"") != -1
		)
			return false;
		return true;
	}
	
	/**
	 * Constructor
	 * 
	 * @param pathname - a path the a file
	 *
	 */
	public MYA_File(String pathname)
	{
		super(pathname);
		m_LastTransmitted = 0;
	}
	
	/**
	 * Constructor
	 * 
	 * @param File - a Java File
	 *
	 */
	public MYA_File(File file)
	{
		super(file.getPath());
		m_LastTransmitted = 0;
	}
	
	/**
	 * sets the last transmission/compile time to current time
	 * 
	 * @param File - a Java File
	 *
	 */
	public void setLastTransmitted()
	{
		Date date = new Date();
		m_LastTransmitted = date.getTime();
		
	}
	
	/**
	 * sets the file to status 'need-transmission'
	 */
	public void setNeedTransmittion()
	{
		m_LastTransmitted = 0;
	}
	
	/**
	 * @return true if the file needs to be re-transmitted/compiled
	 */
	public boolean needToTransmit()
	{
		if (this.lastModified() > m_LastTransmitted)
			return true;
		return false;
	}
	
	/**
	 * @return an Array of all the files under the file.
	 */
	public MYA_File[] listFiles()
	{
		if (this.isFile())
			return null;
		File[] tmpList = super.listFiles();
		MYA_File[] retList = new MYA_File[tmpList.length];
		int i = 0;
		for (File file : tmpList)
		{
			retList[i++] = new MYA_File(file);
		}
		return retList;
	}
	long m_LastTransmitted; //keeps time of last transmission to server
}
